<?php
namespace Admin\Controller;
use Think\Controller;
/**
* 聊天测试
*/
class ChatController extends Controller{

    const   SERVERAPIURL = 'http://api.cn.ronghub.com';    //IM服务地址
    const   SMSURL = 'http://api.sms.ronghub.com';          //短信服务地址
    const   appKey = 'qf3d5gbjqfmih';          
    const   appSecret = 'DULYmvAsQ8BttD';          
	public function Index(){
		// 重置随机数种子。
		srand((double)microtime()*1000000);

		$appSecret = 'DULYmvAsQ8BttD'; // 开发者平台分配的 App Secret。
		$nonce = rand(); // 获取随机数。
		$timestamp = time(); // 获取时间戳。

		$signature = sha1($appSecret.$nonce.$timestamp);

		$this->curlGet(self::SERVERAPIURL);
		$this->display();
	}
	public function getsignature(){
	    $noncestr = 'sdsdsdssd';
	    $jsapi_ticket = $this->getjsticket();
	    $timestamp = time();
	    $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	    $string1 = 'jsapi_ticket='.$jsapi_ticket.'&noncestr='.$noncestr.'&timestamp='.$timestamp.'&url='.$url;
	    $signature = sha1($string1);
	    return $signature;
	}
	/**
     * 创建http header参数
     * @param array $data
     * @return bool
     */
    private function createHttpHeader() {
        $nonce = mt_rand();
        $timeStamp = time();
        $sign = sha1($this->appSecret.$nonce.$timeStamp);
        return array(
                'RC-App-Key:'.$this->appKey,
                'RC-Nonce:'.$nonce,
                'RC-Timestamp:'.$timeStamp,
                'RC-Signature:'.$sign,
        );
    }
	function curlGet($url,$type="POST"){
        $ch = curl_init();
        $header = "Accept-Charset: utf-8";
        $httpHeader = $this->createHttpHeader();
        $httpHeader[] = 'Content-Type:Application/json';
        $params = array (
    		'userId' => 'userId',
    		'name' => 'username',
    		'portraitUri' => 'http://www.rongcloud.cn/images/logo.png'
    		);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params) );
        curl_setopt($ch, CURLOPT_URL, $url.'/user/refresh.json');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $temp = curl_exec($ch);
        curl_close($ch);
        dump($temp);
        return $temp;
    }
}